﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.Practices.ObjectBuilder2;

namespace eCRAM.Tools
{
    public static class ExtensionMethods
    {
        public static string Capitalize(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }

        /// <summary>
        /// Obtient le libellé associé à l'enum
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetLabel(this Enum value)
        {
            return value.GetType().GetField(value.ToString())
                .GetCustomAttributes(false)
                .Where(pi => pi is DisplayAttribute)
                .Select(pi => ((DisplayAttribute) pi).Name)
                .FirstOrDefault()
            ;
        }

        public static IEnumerable<T> FlatList<T>(this IEnumerable<IEnumerable<T>> list)
        {
            var ret = new List<T>();
            list.ForEach(ret.AddRange);
            return ret;
        }
    }
}
